﻿using AntdUI;
using NetTaste;

namespace TestPressure
{
    public partial class DevicePointSelectForm : BaseForm
    {
        #region 自定义方法

        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadList()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            List<DevicePoint> list = SqlSugarHelper.Inquireable<DevicePoint>(where);
            UpdateTable(list);
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<DevicePoint> list)
        {
            var antItemList = new List<DevicePointItem>();
            foreach (DevicePoint item in list)
            {
                string range = item.range_min.ToString() + "-" + item.range_max.ToString();
                string outValue = item.out_min.ToString() + "-" + item.out_max.ToString();
                string alarm = item.alarm_low.ToString() + "-" + item.alarm_hight.ToString();
                string registerType = intToRegisterTypeString(item.register_type);

                antItemList.Add(new DevicePointItem(item.device_number, item.device_name, item.point_number,
                                                    item.point_name, item.point_address, range, outValue, item.amend,
                                                    alarm, item.error_range.ToString(), item.unit, registerType));
            }
            Table.DataSource = antItemList;
        }

        /// <summary>寄存器类型
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private string intToRegisterTypeString(int type)
        {
            string registerType = "";
            if (type == 1)
            {
                registerType = "DI";
            }
            else if (type == 2)
            {
                registerType = "DO";
            }
            else if (type == 3)
            {
                registerType = "InputRegister";
            }
            else if (type == 4)
            {
                registerType = "HoldingRegister";
            }
            return registerType;
        }
        #endregion

        #region 系统方法
        public DevicePointSelectForm()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterParent;
            FullScreen = true;
        }

        private void DevicePointSelectForm_Load(object sender, EventArgs e)
        {
            Header.Text = "设备点位选择";

            Table.Height = Height - Header.Height;
            Table.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("device_number","设备编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("device_name","设备名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("point_number","点位编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("point_name","点位名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("point_address","点位地址",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("range","量程范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("outValue","输出范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("alarm","报警范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("error_range","判断范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("unit","数据单位",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("register_type","寄存器类型",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("operation","操作",AntdUI.ColumnAlign.Center){ Width="120", LineBreak=true}
            };
            Spin.Config config = new Spin.Config
            {
                Text = "正在加载中...",
            };
            Table.Spin(config, () =>
            {
                LoadList();
            });
        }
        private void Table_CellDoubleClick(object sender, TableClickEventArgs e)
        {
            if (this.Owner is not null)
            {
                if (e.Record is not null)
                {
                    DevicePointItem item = (DevicePointItem)e.Record;
                    FormPointAddForm frm = (FormPointAddForm)this.Owner;
                    frm.devicePointObj = item;
                    frm.ReloadDevicePointInfo();
                    this.Close();
                }
            }
            else
            {
                this.Close();
            }
        }
        #endregion
    }
}
